package com.cx.entity;

import java.util.HashMap;

/**
 * @ClassName: RestResp
 * @Description: 返回结果集合
 * @Author: cx
 * @Date: 2023/11/8 13:58
 **/
public class RestResp extends HashMap<String, Object> {

    private static final long serialVersionUID = 1L;

    public static final String CODE_TAG = "code";

    public static final String MSG_TAG = "msg";

    public static final String DATA_TAG = "data";

    public static final String TOTAL_TAG = "total";


    /**
     * 状态类型
     */
    public enum Type {
        SUCCESS(200),

        FAILURE(-200),

        WARN(301),

        PERMISSION(403),


        ERROR(500);

        private final int value;

        Type(int value) {
            this.value = value;
        }
    }

    public RestResp(int total, Object data) {
        super.put(CODE_TAG, total);
        super.put(DATA_TAG, data);
    }


    public RestResp(Type type, String msg, Object data) {
        super.put(CODE_TAG, type.value);
        super.put(MSG_TAG, msg);
        if (data != null) {
            super.put(DATA_TAG, data);
        }
    }

    public RestResp(int code, String msg) {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
    }





    public RestResp(Type type, Object data) {
        super.put(CODE_TAG, type.value);
        if (data != null) {
            super.put(DATA_TAG, data);
        }
    }


    public static RestResp success() {
        return new RestResp(Type.SUCCESS, "操作成功");
    }

    public static RestResp success(String msg) {
        return new RestResp(Type.SUCCESS, msg);
    }

    public static RestResp success(String msg, Object data) {
        return new RestResp(Type.SUCCESS, msg, data);
    }

    public static RestResp success(Integer total,String msg, Object data) {
        RestResp restResp = new RestResp(Type.SUCCESS, msg, data);
        restResp.put(TOTAL_TAG,total);
        return restResp;
    }
    public static RestResp success(Integer total,Object data) {
        RestResp restResp = new RestResp(Type.SUCCESS, "操作成功", data);
        restResp.put(TOTAL_TAG,total);
        return restResp;
    }


    public static RestResp success(Object data) {
        return new RestResp(Type.SUCCESS, data);
    }


    public static RestResp error() {
        return new RestResp(Type.ERROR, "代码错误");
    }

    public static RestResp error(String msg) {
        return new RestResp(Type.ERROR, msg);
    }

    public static RestResp failure() {
        return new RestResp(Type.FAILURE, "不通过");
    }

    public static RestResp warn() {
        return new RestResp(Type.WARN, "警告");
    }

    public static RestResp warn(String msg) {
        return new RestResp(Type.WARN, msg);
    }


    public static RestResp permission() {
        return new RestResp(Type.PERMISSION, "身份认证错误");
    }

    public static RestResp customer(int code, String message) {
        return new RestResp(code, message);
    }

}
